package com.bee.beemanage.api.controller.common;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import com.bee.beemanage.api.entity.output.VerifyCode;
import com.bee.beemanage.system.common.utils.IpUtil;
import com.bee.beemanage.system.common.utils.redis.RedisUtils;
import com.bee.beemanage.system.common.utils.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import static com.bee.beemanage.system.common.utils.result.ResultTool.success;

@RestController
@RequestMapping("/bee-manage/verifyCode")
@Api(value ="验证码",tags = "验证码")
public class VerifyCodeController {

    @Resource
    RedisUtils redisUtils;

    @GetMapping
    @ApiOperation(value = "获取验证码", notes = "获取验证码")
    public Result createCaptcha(HttpServletRequest request) {
        String realIp = IpUtil.getIpAddr(request);
        VerifyCode verifyCode = new VerifyCode();
        // 定义图形验证码的长和宽
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(100, 50);
        //转字符串
        redisUtils.set("VerifyCode:"+realIp, lineCaptcha.getCode(), 60);
        verifyCode.setImg(lineCaptcha.getImageBase64());
        verifyCode.setCodeId(realIp);
        return success(verifyCode);
    }
}
